﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using IterationLife.Extensions.Dtos;
using IterationLife.FootballTeaching.Enums;
using IterationLife.FootballTeaching.ItemModels;
using Util.Domains;
using Util.Domains.Auditing;

namespace IterationLife.FootballTeaching.Models
{
    /// <summary>
    /// 课程
    /// </summary>
    [Description("课程")]
    public partial class Course : AggregateRoot<Course>, IDelete, IAudited
    {
        /// <summary>
        /// 初始化课程
        /// </summary>
        public Course() : this(Guid.Empty)
        {
        }

        /// <summary>
        /// 初始化课程
        /// </summary>
        /// <param name="id">课程标识</param>
        public Course(Guid id) : base(id)
        {
            Chapters = new List<Chapter>();
            UserCourses = new List<UserCourse>();
            CourseCategories = new CourseCategories();
            PlayerPositions = new PlayerPositions();
            Banner = new Resources();
        }

        /// <summary>
        /// 课程分类集合
        /// </summary>
        [NotMapped]
        public CourseCategories CourseCategories { get; set; }

        /// <summary>
        /// 课程分类集合Json字符串
        /// </summary>
        [Required(ErrorMessage = "课程分类集合不能为空")]
        public string CourseCategoriesJsonStr
        {
            get => Util.Json.ToJson(CourseCategories);
            set => CourseCategories = Util.Json.ToObject<CourseCategories>(value);
        }

        /// <summary>
        /// 球员位置集合
        /// </summary>
        [NotMapped]
        public PlayerPositions PlayerPositions { get; set; }

        /// <summary>
        /// 球员位置集合Json字符串
        /// </summary>
        [Required(ErrorMessage = "球员位置集合不能为空")]
        public string PlayerPositionsJsonStr
        {
            get => Util.Json.ToJson(PlayerPositions);
            set => PlayerPositions = Util.Json.ToObject<PlayerPositions>(value);
        }
        /// <summary>
        /// 名称
        /// </summary>
        [Required(ErrorMessage = "名称不能为空")]
        [StringLength(50, ErrorMessage = "名称输入过长，不能超过50位")]
        public string Name { get; set; }
        /// <summary>
        /// 封面图
        /// </summary>
        [NotMapped]
        public Resources Banner { get; set; }
        /// <summary>
        /// 封面图
        /// </summary>
        [StringLength(500, ErrorMessage = "封面图输入过长，不能超过500位")]
        public string BannerJsonStr
        {
            get => Util.Json.ToJson(Banner);
            set => Banner = Util.Json.ToObject<Resources>(value);
        }
        /// <summary>
        /// 难度
        /// </summary>
        [Required(ErrorMessage = "难度不能为空")]
        public DifficultyType? Difficulty { get; set; }
        /// <summary>
        /// 动作数
        /// </summary>
        [Required(ErrorMessage = "动作数不能为空")]
        public int ActionNumber { get; set; }
        /// <summary>
        /// 参与人数
        /// </summary>
        [Required(ErrorMessage = "参与人数不能为空")]
        public long ParticipationNumber { get; set; }
        /// <summary>
        /// 排序号
        /// </summary>
        public int? SortId { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreationTime { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        public Guid? CreatorId { get; set; }
        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }
        /// <summary>
        /// 最后修改人
        /// </summary>
        public Guid? LastModifierId { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        [Required(ErrorMessage = "是否删除不能为空")]
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 章节列表
        /// </summary>
        public virtual ICollection<Chapter> Chapters { get; set; }

        /// <summary>
        /// 用户课程列表
        /// </summary>
        public virtual ICollection<UserCourse> UserCourses { get; set; }

        /// <summary>
        /// 添加描述
        /// </summary>
        protected override void AddDescriptions()
        {
            AddDescription("课程编号", Id);
            AddDescription("课程分类集合", CourseCategoriesJsonStr);
            AddDescription("球员位置集合", PlayerPositions);
            AddDescription("名称", Name);
            AddDescription("封面图", Banner);
            AddDescription("难度", Difficulty);
            AddDescription("动作数", ActionNumber);
            AddDescription("参与人数", ParticipationNumber);
            AddDescription("排序号", SortId);
            AddDescription("创建时间", CreationTime);
            AddDescription("创建人", CreatorId);
            AddDescription("最后修改时间", LastModificationTime);
            AddDescription("最后修改人", LastModifierId);
        }

        /// <summary>
        /// 添加变更列表
        /// </summary>
        protected override void AddChanges(Course newEntity)
        {
            AddChange("Id", "课程编号", Id, newEntity.Id);
            AddChange("CourseCategoriesJson", "课程分类集合", CourseCategoriesJsonStr, newEntity.CourseCategoriesJsonStr);
            AddChange("PlayerPositions", "球员位置集合", PlayerPositions, newEntity.PlayerPositions);
            AddChange("Name", "名称", Name, newEntity.Name);
            AddChange("Banner", "封面图", Banner, newEntity.Banner);
            AddChange("Difficulty", "难度", Difficulty, newEntity.Difficulty);
            AddChange("ActionNumber", "动作数", ActionNumber, newEntity.ActionNumber);
            AddChange("ParticipationNumber", "参与人数", ParticipationNumber, newEntity.ParticipationNumber);
            AddChange("SortId", "排序号", SortId, newEntity.SortId);
            AddChange("CreationTime", "创建时间", CreationTime, newEntity.CreationTime);
            AddChange("CreatorId", "创建人", CreatorId, newEntity.CreatorId);
            AddChange("LastModificationTime", "最后修改时间", LastModificationTime, newEntity.LastModificationTime);
            AddChange("LastModifierId", "最后修改人", LastModifierId, newEntity.LastModifierId);
        }
    }
}